package cheme.ProcessModel.Visitors;

import java.util.HashMap;
import java.util.Map;

import cheme.Interpreter.AST.SymbolTable.SymbolTable;
import cheme.Interpreter.AST.SystemSolver.ChemicalVisitor;
import cheme.Interpreter.AST.SystemSolver.EquationVisitor;
import cheme.Interpreter.AST.SystemSolver.StreamDeclarationVisitor;
import cheme.ProcessModel.Equations.Equation;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.OverallTagComponent;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.TagComponent;

/*
 * TODO: really no need for this to be a singleton over static... fix that!
 */
public class ParseTreeVisitorFactory implements IParseTreeVisitorFactory
{
	private static IParseTreeVisitorFactory instance = null;
	private static Map<Object, ChemicalVisitor> visitorMap = null;
	
	private ParseTreeVisitorFactory()
	{
	}
	
	public static IParseTreeVisitorFactory getInstance()
	{
		if(null == instance)
		{
			instance = new ParseTreeVisitorFactory();
		}
		
		return instance;
	}
	
	private static Map<Object, ChemicalVisitor> getVisitorMap()
	{
		if(null == visitorMap)
		{
			visitorMap = new HashMap<Object, ChemicalVisitor>();
		}
		
		return visitorMap;
	}
	
	public ChemicalVisitor getParseTreeVisitor(Object type, SymbolTable symbolTable)
	{
		ChemicalVisitor visitor = getVisitorMap().get(type);
		
		if(null == visitor)
		{
			if(type instanceof OverallTagComponent || type instanceof TagComponent)
			{
				visitor = new StreamDeclarationVisitor(symbolTable);
			}
			else if(type instanceof Equation)
			{
				visitor = new EquationVisitor(symbolTable);
			}
			else
			{
				visitor = null;
			}
			
			getVisitorMap().put(type, visitor);
		}
		
		return visitor;
	}
}
